package cn.xiaomubiao.oschine.viewpagerfragment.tweetspagerfragment;

import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.text.Html;
import android.text.Spanned;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.bumptech.glide.Glide;
import com.jcodecraeer.xrecyclerview.XRecyclerView;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import cn.xiaomubiao.oschine.MyApplication;
import cn.xiaomubiao.oschine.R;
import cn.xiaomubiao.oschine.adapter.tweets_pager_adapter.TweetsBaseRecyclerAdapter;
import cn.xiaomubiao.oschine.adapter.tweets_pager_adapter.TweetsDetailRecyclerAdapter;
import cn.xiaomubiao.oschine.base.BaseEachFragment;
import cn.xiaomubiao.oschine.bean.Comment;
import cn.xiaomubiao.oschine.bean.CommentList;
import cn.xiaomubiao.oschine.bean.Tweet;
import cn.xiaomubiao.oschine.bean.TweetDetail;
import cn.xiaomubiao.oschine.contants.Host;
import cn.xiaomubiao.oschine.util.HttpUtil;
import de.hdodenhof.circleimageview.CircleImageView;


/**
 * Created by hasee on 2017/4/6.
 */

public class TweetsDetailFragment extends BaseEachFragment {
    @BindView(R.id.recy_tweets_detail)
    XRecyclerView mRecyTweetsDetail;
    @BindView(R.id.edt_comment)
    EditText mEdtComment;

    /*头部View的控件*/
    CircleImageView mIvAvatar;
    TextView mTvUsername;
    TextView mTvTime;
    TextView mTvContent;

    private Tweet mTweet;
    private ImageView mIvBig;
    private List<Comment> mComments;
    private View mHeaderView;


    @Override
    public View loadSuccess() {
        View view = View.inflate(getContext(), R.layout.tweets_detail_fragment, null);
        ButterKnife.bind(this, view);

        initHeaderView();
        initRecylerView();

        return view;
    }

    private void initRecylerView() {
        mRecyTweetsDetail.addHeaderView(mHeaderView);
        mRecyTweetsDetail.setAdapter(new TweetsDetailRecyclerAdapter(mComments));
        mRecyTweetsDetail.setLayoutManager(new LinearLayoutManager(getContext()));
    }

    private void initHeaderView() {
        mHeaderView = View.inflate(getContext(), R.layout.view_tweets_detail_header, null);
        mIvAvatar = (CircleImageView) mHeaderView.findViewById(R.id.iv_avatar);
        mTvUsername = (TextView) mHeaderView.findViewById(R.id.tv_username);
        mTvTime = (TextView) mHeaderView.findViewById(R.id.tv_time);
        mTvContent = (TextView) mHeaderView.findViewById(R.id.tv_content);
        mIvBig = (ImageView) mHeaderView.findViewById(R.id.iv_imageBig);

        //设置头像
        if(mTweet.getPortrait().length() > 0) {
            Glide.with(getContext()).load(mTweet.getPortrait()).into(mIvAvatar);
        } else {
            Glide.with(getContext()).load(R.drawable.widget_dface).into(mIvAvatar);
        }
        //用户名
        mTvUsername.setText(mTweet.getAuthor());
        //内容
        Spanned spanned = Html.fromHtml(mTweet.getBody());
        mTvContent.setText(spanned);
        //时间
        mTvTime.setText(mTweet.getPubDate());
        //图片
        if(mTweet.getImgBig().length() > 0) {
            mIvBig.setVisibility(View.VISIBLE);
            Glide.with(getContext()).load(mTweet.getImgBig()).into(mIvBig);
        } else {
            mIvBig.setVisibility(View.GONE);
        }
    }

    @Override
    public void loadData() {
        Bundle arguments = getArguments();
        int id = (int) arguments.get("id");
        String url = Host.HOST_DEFAULT+"detail/tweet_detail/" + id + ".xml";
        String commentUrl =  Host.HOST_DEFAULT+"commentlist/catalog3id" + id + "page" + 0 + ".xml";

        /*
        *   获取详情页头部布局的个人数据
        * */
        HttpUtil<TweetDetail> tweetHttpUtil = new HttpUtil<>(getContext(),TweetDetail.class);
        tweetHttpUtil.getStringData(url, new HttpUtil.OnGetDataListener<TweetDetail>() {
            @Override
            public void onSuccess( TweetDetail tweetDetail) {
                mTweet = tweetDetail.getTweet();
                mCommonPager.showSuccessPager(mTweet);

            }

            @Override
            public void onFailure( Exception e ) {
               mCommonPager.showFailPager();
            }
        });

        /*
        *   获取详情页列表布局的评论数据
         */
        HttpUtil<CommentList> commentListHttpUtil = new HttpUtil<>(getContext(),CommentList.class);
        commentListHttpUtil.getStringData(commentUrl, new HttpUtil.OnGetDataListener<CommentList>() {
            @Override
            public void onSuccess( CommentList commentList ) {
                mComments = commentList.getList();
            }

            @Override
            public void onFailure( Exception e ) {
                mCommonPager.showFailPager();
            }
        });



    }

    @OnClick({R.id.iv_emoji, R.id.iv_send_message})
    public void onClick( View view ) {
        switch (view.getId()) {
            case R.id.iv_emoji:
                break;
            case R.id.iv_send_message:
                break;
        }
    }

}
